﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.Mvc.Html;
using System.Web.Mvc;
using System.Web;

namespace QuickApp.MVCControl.FlexiGrid
{
    /// <summary>
    /// 泛型的FlexiGrid设置类
    /// </summary>
    /// <typeparam name="T" >Model对象类型</typeparam>
    public class FlexiGridSettings<T> : IHtmlString where T : class
    {
        #region Private Fields

        /// <summary>
        /// 输出表格HTML的实例
        /// </summary>
        private IGridRenderer<FlexiGridSettings<T>> _renderer;
        #endregion

        #region Constructor

        /// <summary>
        /// 构造函数，初始化<see cref="FlexiGridSettings&lt;T&gt;"/> 类，设置输出表格HTML的实例
        /// </summary>
        public FlexiGridSettings()
        {
            EnableRecordsPerPage = false;
            EnableTableToggleButton = false;
            EnablePager = true;
            EnableShowCheckBox = true;
            EnableSelectedOnClick = false;

            GridColumns = new FlexiGridColumnCollection<T>();
            GridButtons = new FlexiGridButtonCollection<T>();
            this._renderer = new FlexiGridRenderer<T>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// 高度
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string GridHeight { get; private set; }

        /// <summary>
        /// 宽度
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string GridWidth { get; private set; }

        /// <summary>
        /// 排序规则
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal FlexiGridSortOrder DefaultSortOrder { get; private set; }

        /// <summary>
        /// 默认排序字段
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string DefaultSortField { get; private set; }

        /// <summary>
        /// 是否分页
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal bool EnablePager { get; private set; }

        /// <summary>
        /// 每页默认的结果数
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal int PageSize { get; private set; }

        /// <summary>
        /// 显示/隐藏Grid的按钮是否有效
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal bool EnableTableToggleButton { get; private set; }

        /// <summary>
        /// 是否可以动态设置每页显示的结果数
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal bool EnableRecordsPerPage { get; private set; }

        /// <summary>
        /// 表格标题
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string GridTitle { get; private set; }

        /// <summary>
        /// 表格的数据格式
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal FlexiGridDataType GridDataType { get; private set; }

        /// <summary>
        /// 请求服务端数据的URL
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string ActionUrl { get; private set; }

        /// <summary>
        /// 关联编辑表单的URL
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string FormActionUrl { get; private set; }

        /// <summary>
        /// 表格ID
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string GridId { get; private set; }

        /// <summary>
        /// 请求服务端数据的URL
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal bool EnableAutoLoad { get; private set; }

        /// <summary>
        /// 是否显示选择框（checkbox）
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal bool EnableShowCheckBox { get; private set; }

        /// <summary>
        /// 点击行即选中该行数据
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal bool EnableSelectedOnClick { get; private set; }

        /// <summary>
        /// 当行被选中时触发的事件，这里指checkbox选中
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal string GridRowChecked { get; private set; }

        /// <summary>
        /// 扩展外部参数动态注册到grid，实现如自定义查询等操作  
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal IDictionary<string, object> GridExtParam { get; private set; }

        /// <summary>
        /// 列信息集合
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal FlexiGridColumnCollection<T> GridColumns { get; private set; }

        /// <summary>
        /// 按钮信息集合
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        internal FlexiGridButtonCollection<T> GridButtons { get; private set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// 当行被选中时触发的事件，这里指checkbox选中
        /// </summary>
        /// <param name="function">触发事件</param>
        /// <returns></returns>
        public FlexiGridSettings<T> OnRowChecked(string function)
        {
            GridRowChecked = function;
            return this;
        }

        /// <summary>
        /// 设置要生成Grid的Id
        /// </summary>
        /// <param name="elementId">HTML元素ID</param>
        /// <returns></returns>
        public FlexiGridSettings<T> ID(string elementId)
        {
            GridId = elementId;
            return this;
        }

        /// <summary>
        /// 请求服务端数据的URL
        /// </summary>
        /// <param name="actionUrl">Action地址</param>
        /// <returns></returns>
        public FlexiGridSettings<T> Action(string actionUrl)
        {
            ActionUrl = actionUrl;
            return this;
        }

        /// <summary>
        /// 关联编辑表单的URL
        /// </summary>
        /// <param name="actionUrl">Action地址</param>
        /// <returns></returns>
        public FlexiGridSettings<T> FormAction(string actionUrl)
        {
            FormActionUrl = actionUrl;
            return this;
        }

        /// <summary>
        /// 传递数据的类型
        /// </summary>
        /// <param name="dataType">数据类型 Xml 或 Json，默认为Json</param>
        /// <returns></returns>
        public FlexiGridSettings<T> DataType(FlexiGridDataType dataType)
        {
            GridDataType = dataType;
            return this;
        }

        /// <summary>
        /// 设置表格的标题
        /// </summary>
        /// <param name="title">标题</param>
        /// <returns></returns>
        public FlexiGridSettings<T> Title(string title)
        {
            GridTitle = title;
            return this;
        }

        /// <summary>
        /// 允许自动加载
        /// </summary>
        /// <returns></returns>
        public FlexiGridSettings<T> AutoLoad()
        {
            EnableAutoLoad = true;
            return this;
        }

        /// <summary>
        /// 设置每页显示的结果数
        /// </summary>
        /// <param name="pageSize">每页记录数</param>
        /// <returns></returns>
        public FlexiGridSettings<T> SetPageSize(int pageSize)
        {
            PageSize = pageSize;
            return this;
        }

        /// <summary>
        /// 设置显示/隐藏Grid的按钮是否有效
        /// </summary>
        /// <param name="show">是否显示</param>
        /// <returns></returns>
        public FlexiGridSettings<T> ShowTableToggleButton(bool show)
        {
            EnableTableToggleButton = show;
            return this;
        }

        /// <summary>
        /// 是否显示选择框（checkbox）
        /// </summary>
        /// <param name="show">是否显示</param>
        /// <returns></returns>
        public FlexiGridSettings<T> ShowCheckBox(bool show = true)
        {
            EnableShowCheckBox = show;
            return this;
        }

        /// <summary>
        /// 点击行即选中该行数据
        /// </summary>
        /// <param name="enable">是否启用</param>
        /// <returns></returns>
        public FlexiGridSettings<T> SelectedOnClick(bool enable = true)
        {
            EnableSelectedOnClick = enable;
            return this;
        }

        /// <summary>
        /// 设置是否为默认分页
        /// </summary>
        /// <returns></returns>
        public FlexiGridSettings<T> UsePager(bool enable = true)
        {
            EnablePager = enable;
            return this;
        }

        /// <summary>
        /// 默认排序的字段名，及排序方式
        /// </summary>
        /// <param name="fieldName">字段名称</param>
        /// <param name="order">排序规则</param>
        /// <returns></returns>
        public FlexiGridSettings<T> DefaultSortOption(string fieldName, FlexiGridSortOrder order = FlexiGridSortOrder.Desc)
        {
            DefaultSortField = fieldName;
            DefaultSortOrder = order;
            return this;
        }

        /// <summary>
        /// 设置表格的列
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public FlexiGridSettings<T> Columns(Action<FlexiGridColumnCollection<T>> action)
        {
            action(GridColumns);
            return this;
        }

        /// <summary>
        /// 设置表格的按钮
        /// </summary>
        /// <param name="action"></param>
        /// <returns></returns>
        public FlexiGridSettings<T> Buttons(Action<FlexiGridButtonCollection<T>> action)
        {
            action(GridButtons);
            return this;
        }

        /// <summary>
        /// 设置表格宽度
        /// </summary>
        /// <param name="width">宽度，默认自适应</param>
        /// <returns></returns>
        public FlexiGridSettings<T> Width(int width)
        {
            GridWidth = width.ToString();
            return this;
        }

        /// <summary>
        /// 设置表格宽度
        /// </summary>
        /// <param name="width">宽度，可以是变量</param>
        /// <returns></returns>
        public FlexiGridSettings<T> Width(string width)
        {
            GridWidth = width;
            return this;
        }

        /// <summary>
        /// 设置表格高度
        /// </summary>
        /// <param name="height">高度，自适应高度</param>
        /// <returns></returns>
        public FlexiGridSettings<T> Height(int height)
        {
            GridHeight = height.ToString();
            return this;
        }

        /// <summary>
        /// 设置表格高度
        /// </summary>
        /// <param name="height">高度，可以是变量</param>
        /// <returns></returns>
        public FlexiGridSettings<T> Height(string height)
        {
            GridHeight = height;
            return this;
        }

        /// <summary>
        /// 扩展外部参数动态注册到grid，实现如自定义查询等操作
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public FlexiGridSettings<T> ExtParam(IDictionary<string, object> dicParams)
        {
            GridExtParam = dicParams;
            return this;
        }

        /// <summary>
        /// 扩展外部参数动态注册到grid，实现如自定义查询等操作
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public FlexiGridSettings<T> ExtParam(object param)
        {
            GridExtParam = HtmlHelper.AnonymousObjectToHtmlAttributes(param);
            return this;
        }

        #endregion

        #region IHtmlString Members

        public string ToHtmlString()
        {
            return this._renderer.Render(this);
        }

        public override string ToString()
        {
            return this._renderer.Render(this);
        }
        #endregion
    }
}
